﻿Imports System.Data.OleDb

Public Class QuizGameTimeManagement
    Inherits ConnectionClass

    Public Sub InsertQuizGameTime(ByVal quizId As Int32, ByVal userId As Int32, ByVal quizGameTime As Int32)
        Try

        
        myConnection.Open()
        Dim SqlString As String = "INSERT INTO QuizesGameTime(QuizId,UserId, QuizGameTimeSeconds) VALUES(@f1,@f2,@f3)"
        Dim newCmd As OleDbCommand = New OleDbCommand(SqlString, myConnection)
        newCmd.CommandType = CommandType.Text
        newCmd.Parameters.AddWithValue("@f1", quizId)
        newCmd.Parameters.AddWithValue("@f2", userId)
        newCmd.Parameters.AddWithValue("@f3", quizGameTime)

        newCmd.ExecuteNonQuery()
            myConnection.Close()
        Catch ex As Exception

        End Try
    End Sub


    Public Function SelectAverageTimeTakenByUser(ByVal userId As Int32) As OleDbDataReader

        Try

        
        myConnection.Open()
        Dim SqlString As String = "SELECT AVG(QuizGameTimeSeconds) AS AverageTimeTaken FROM   QuizesGameTime WHERE  (UserId = @f1) GROUP BY UserId"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, myConnection)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", userId)
        Dim dr As OleDbDataReader = cmd.ExecuteReader()
        Return dr
        myConnection.Close()
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

End Class
